package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"orders/cache"
	"orders/models"
)

type OrdersController struct {
	beego.Controller
}
type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}
type PageResponse struct {
	Code    int         `json:"code"`
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
	Page    int         `json:"page"`
	Size    int         `json:"size"`
	Count   float64     `json:"count"`
	Sumpage int         `json:"sumpage"`
}

/*func (c *OrdersController) List() {

	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	var count int64
	models.DB.Model(&models.Orders{}).Count(&count)
	sumpage := int(math.Ceil(float64(count) / float64(size)))
	if page > sumpage {
		page = sumpage
	}

}*/

func (c *OrdersController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var orders []*models.Orders
	cacheData := cache.Rdb.Get(cache.Ctx, "orders").Val()
	if cacheData == "" {
		jsonData, _ := json.Marshal(orders)
		cache.Rdb.Set(cache.Ctx, "orders", jsonData, 0)
	} else {
		json.Unmarshal([]byte(cacheData), &orders)
	}
	c.Data["json"] = Response{
		Code: 200,
		Msg:  "获取成功",
		Data: orders,
	}
}
